package ca.etsmtl.log430.lab3.SystemeB;

import java.io.PipedWriter;

/**
 * This class contains the main method for assignment 3. The program consists of
 * these files:<br>
 * <br>
 * 
 * 1) SystemInitialize: instantiates all filters and pipes, starts all filters.<br>
 * 2) FileReaderFilter: reads input file and sends each line to its output pipe.<br>
 * 3) StatusFilter: separates the input stream in two project types (REG, CRI)
 * and writes lines to the appropriate output pipe.<br>
 * 4) StateFilter: determines if an entry contains a particular state specified
 * at instantiation. If so, sends the whole line to its output pipe.<br>
 * 5) MergeFilter: accepts inputs from 2 input pipes and writes them to its
 * output pipe.<br>
 * 6) FileWriterFilter: sends its input stream to a text file.<br>
 * <br>
 * 
 * Pseudo Code:
 * 
 * <pre>
 * instantiate all filters and pipes
 * start FileReaderFilter
 * start StatusFilter
 * start StateFilter for RIS
 * start StateFilter for DIF
 * start MergeFilter
 * start FileWriterFilter
 * </pre>
 * 
 * Running the program:
 * 
 * <pre>
 * java SystemInitialize InputFile OutputFile > DebugFile
 * 
 * SystemInitialize - Program name
 * InputFile - Text input file (see comments below)
 * OutputFile - Text output file with students
 * DebugFile - Optional file to direct debug statements
 * </pre>
 * 
 * @author A.J. Lattanze
 */

public class SystemInitialize {

	public static void main(String argv[]) {
		// Let's make sure that input and output files are provided on the
		// command line

		if (argv.length != 2) {

			System.out
					.println("\n\nNombre incorrect de parametres d'entree. Utilisation:");
			System.out
					.println("\njava SystemInitialize <fichier d'entree> <fichier de sortie>");

		} else {
			// For a second generated file
			String outputFile = argv[1].split("\\.")[0];
			outputFile += "2.txt";

			// These are the declarations for the pipes.
			PipedWriter pipe01 = new PipedWriter();
			PipedWriter pipe02 = new PipedWriter();
			PipedWriter pipe03 = new PipedWriter();
			PipedWriter pipe04 = new PipedWriter();
			PipedWriter pipe05 = new PipedWriter();
			PipedWriter pipe06 = new PipedWriter();
			PipedWriter pipe07 = new PipedWriter();
			PipedWriter pipe08 = new PipedWriter();
			PipedWriter pipe09 = new PipedWriter();
			PipedWriter pipe10 = new PipedWriter();
			PipedWriter pipe11 = new PipedWriter();
			
			PipedWriter pipe12 = new PipedWriter();
			PipedWriter pipe13 = new PipedWriter();
			PipedWriter pipe14 = new PipedWriter();
			PipedWriter pipe15 = new PipedWriter();
			PipedWriter pipe16 = new PipedWriter();
			PipedWriter pipe17 = new PipedWriter();
			PipedWriter pipe18 = new PipedWriter();
			PipedWriter pipe19 = new PipedWriter();
			PipedWriter pipe20 = new PipedWriter();
			
			
			// Instantiate Filter Threads
			Thread fileReaderFilter = new FileReaderFilter(argv[0], pipe01);
			Thread statusFilter = new StatusFilter(pipe01, pipe02, pipe03);
			Thread stateFilter1 = new StateFilter("RIS", pipe02, pipe04, pipe07); //reg et ris  et cri et ris
			Thread stateFilter2 = new StateFilter("DIF", pipe03, pipe05, pipe08); //reg et dif et cri et dif 
			
			Thread mergeFilter = new MergeFilter(pipe04, pipe08, pipe06);
			Thread mergeFilter2 = new MergeFilter(pipe07, pipe05, pipe09);
			Thread fileWriterFilter = new FileWriterFilter(argv[1], pipe06);
			Thread fileWriterFilter2 = new FileWriterFilter(outputFile, pipe09);

			Thread fileReaderFilter2 = new FileReaderFilter(argv[0], pipe12);
			Thread statusFilter2 = new StatusFilter(pipe12,pipe13,pipe14);
			
			//REG
			Thread ratesFilter = new RatesFilter(50,"lesser", pipe13, pipe15);
			
			//CRI
			Thread stateFilter3 = new StateFilter("RIS", pipe14, pipe17, pipe18);
			Thread ratesFilter2 = new RatesFilter(25,"equals", pipe17, pipe16);
			Thread ratesFilter3 = new RatesFilter(75, "greater", pipe18, pipe19);
			
			Thread mergeFilter3 = new MergeFilter(pipe16, pipe19, pipe20);
			
			
			Thread fileWriterFilter3 = new FileWriterFilter("b.txt", pipe15);
			Thread fileWriterFilter4 = new FileWriterFilter("b2.txt",pipe20);
			
			// Start the threads
			fileReaderFilter.start();
			statusFilter.start();
			stateFilter1.start();
			stateFilter2.start();
			mergeFilter.start();
			mergeFilter2.start();
			
			fileWriterFilter.start();
			fileWriterFilter2.start();
			
			fileReaderFilter2.start();
			statusFilter2.start();
			ratesFilter.start();			
			stateFilter3.start();
			ratesFilter2.start();
			ratesFilter3.start();
			mergeFilter3.start();
			fileWriterFilter3.start();
			fileWriterFilter4.start();

		} // if

	} // main

} // SystemInitialize